package com.zimmur.app.mocolink.background.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zimmur.app.mocolink.background.common.PropertiesCommon;
import com.zimmur.app.mocolink.background.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.background.common.StatusCodeEnum;
import com.zimmur.app.mocolink.domain.param.GroupGoodsParam;
import com.zimmur.app.mocolink.domain.param.TopInGroupGoodsReqParam;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.GoodsInfo;
import com.zimmur.app.mocolink.domain.vo.GroupGoodsInfo;
import com.zimmur.app.mocolink.domain.vo.GroupGoodsItemInfo;
import com.zimmur.app.mocolink.domain.vo.GroupGoodsVo;
import com.zimmur.app.mocolink.domain.vo.GroupSceneVo;
import com.zimmur.app.mocolink.domain.vo.GroupStyleVo;
import com.zimmur.app.mocolink.domain.vo.UserCommentInfo;
import com.zimmur.app.mocolink.service.IGroupGoodsService;
import com.zimmur.app.mocolink.service.IGroupSceneService;
import com.zimmur.app.mocolink.service.IGroupStyleService;
import com.zimmur.app.mocolink.service.IUserCommentService;

@RestController
@RequestMapping("/restful/groupGoods")
public class GroupGoodsController extends BaseController{

	@Autowired
	private IGroupGoodsService groupGoodsService;
	@Autowired
	private IGroupStyleService groupStyleService;
	@Autowired
	private IGroupSceneService groupSceneService;
	@Autowired
	private IUserCommentService userCommentService;
	/**
	* <p>Title: queryTopInGroupGoods</p>
	* <p>Description: 最in组货
	* 	key的值：
	* 		1->按照创建时间排序来查询
	* 		2->按照点击、收藏、评论依次降序查询
	* 		3->无排序条件查询(全部查询)
	* </p>
	* @return
	 */
	@RequestMapping("/queryTopInGroupGoods")
	public ReturnJsonEntity queryTopInGroupGoods(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer key = getIntParam("key");
		Integer userId = getTokenUserId();
		Integer pageIndex = getParam("pageIndex",1);
		Integer pageSize = getParam("pageSize",10);
		String orderByClause ;
		TopInGroupGoodsReqParam param = new TopInGroupGoodsReqParam();
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		param.setUserId(userId);
		if(key!=null && key==1){//最新查询
			orderByClause = "a.create_time DESC";
		}else if(key!=null &&  key==2){//最热查询
			orderByClause = "a.brower_num DESC,a.collect_quantity DESC,a.comment_quantity DESC";
		}else {//全部查询
			orderByClause = "a.comment_quantity DESC,a.id desc";
		}
		param.setOrderByClause(orderByClause);
		
		PageUtil<GroupGoodsInfo> pageList = groupGoodsService.queryTopInGroupGoods(param);
		if(pageList!=null){
			entity.setData(pageList);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	/**
	* <p>Title: getStyleGroupGoods</p>
	* <p>Description:查询风格组货信息 </p>
	* @return
	 */
	@RequestMapping("/styleIndex")
	public ReturnJsonEntity styleIndex(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		Integer pageIndex = getParam("pageIndex",1);
		Integer pageSize = getParam("pageSize",10);
		GroupGoodsParam param = new GroupGoodsParam();
		param.setPageNo(pageIndex);
		param.setPageSize(pageSize);
		param.setUserId(userId);
		String style=null;
		List<GroupStyleVo> styleList = groupStyleService.getAllStyle();
		if(styleList!=null && styleList.get(0).getChildrenList()!=null){
			style=styleList.get(0).getChildrenList().get(0).getName();
		}
		param.setStyle(style);
		PageUtil<GroupGoodsInfo> page = groupGoodsService.queryInfoByParam(param);
		Map<String,Object> map = new HashMap<>();
		map.put("styleList", styleList);
		map.put("groupGoods", page);
		map.put("style", style);
		entity.setData(map);
		codeEnum = StatusCodeEnum.CODE000000;
		
		entity.init(codeEnum);
		return entity;
	}
	/**
	* <p>Title: getGroupGoodsByStyleName</p>
	* <p>Description:通过风格名称查询风格组货信息 </p>
	* @return
	 */
	@RequestMapping("/queryStyleGroupGoods")
	public ReturnJsonEntity getGroupGoodsByStyleName(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		Integer pageIndex = getParam("pageIndex",1);
		Integer pageSize = getParam("pageSize",10);
		String style=getParam("style");
		GroupGoodsParam param = new GroupGoodsParam();
		param.setPageNo(pageIndex);
		param.setPageSize(pageSize);
		param.setUserId(userId);
		if(!StringUtils.isBlank(style)){
			param.setStyle(style);
		}
		PageUtil<GroupGoodsInfo> page = groupGoodsService.queryInfoByParam(param);
		if(page!=null){
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	/**
	* <p>Title: getThemeGroupGoods</p>
	* <p>Description: 场景组货首页</p>
	* @return
	 */
	@RequestMapping("/sceneIndex")
	public ReturnJsonEntity sceneIndex(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		Integer pageIndex = getParam("pageIndex",1);
		Integer pageSize = getParam("pageSize",10);
		GroupGoodsParam param = new GroupGoodsParam();
		param.setPageNo(pageIndex);
		param.setPageSize(pageSize);
		param.setUserId(userId);
		String scene=null;
		List<GroupSceneVo> sceneList = groupSceneService.getAllScene();
		if(sceneList!=null && sceneList.get(0).getChildrenList()!=null){
			scene=sceneList.get(0).getChildrenList().get(0).getName();
		}
		param.setScene(scene);
		PageUtil<GroupGoodsInfo> page = groupGoodsService.queryInfoByParam(param);
		Map<String,Object> map = new HashMap<>();
		map.put("sceneList", sceneList);
		map.put("groupGoods", page);
		map.put("scene", scene);
		entity.setData(map);
		codeEnum = StatusCodeEnum.CODE000000;
		
		entity.init(codeEnum);
		return entity;
	}
	/**
	* <p>Title: getGroupGoodsBySceneName</p>
	* <p>Description: 场景组货分页</p>
	* @return
	 */
	@RequestMapping("/querySceneGroupGoods")
	public ReturnJsonEntity querySceneGroupGoods(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		Integer pageIndex = getParam("pageIndex",1);
		Integer pageSize = getParam("pageSize",10);
		String scene=getParam("scene");
		GroupGoodsParam param = new GroupGoodsParam();
		param.setPageNo(pageIndex);
		param.setPageSize(pageSize);
		param.setUserId(userId);
		if(!StringUtils.isBlank(scene)){
			param.setScene(scene);
		}
		PageUtil<GroupGoodsInfo> page = groupGoodsService.queryInfoByParam(param);
		if(page!=null){
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	
	@RequestMapping("/getGroupGoodsDetail")
	public ReturnJsonEntity getGroupGoodsDetail(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		Integer groupGoodsId = getParam("groupGoodsId", 0);
		GroupGoodsVo groupGoodsVo = groupGoodsService.selectGroupGoodsVo(groupGoodsId, userId);
		if(groupGoodsVo!=null){
			changePrice(groupGoodsVo);
			PageUtil<UserCommentInfo> page = userCommentService.selectByGroupGoodsId(groupGoodsId, 1);
			Map<String, Object> map =new HashMap<>();
			map.put("groupGoodsInfo", groupGoodsVo);
			map.put("commentData", page);
			entity.setData(map);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/queryComment")
	public ReturnJsonEntity queryComment(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer groupGoodsId = getParam("groupGoodsId", 0);
		Integer pageNo = getParam("pageNo", 1);
		PageUtil<UserCommentInfo> page = userCommentService.selectByGroupGoodsId(groupGoodsId, pageNo);
		if(page!=null){
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	public void changePrice(GroupGoodsVo groupGoodsVo){
		String pubUserId = PropertiesCommon.getProperty("public.userId");
		int pUserId = Integer.valueOf(pubUserId);
		Integer userId = getTokenUserId();
		if(userId==null || userId==pUserId){
			groupGoodsVo.setTotalPrice("***");
			List<GroupGoodsItemInfo> goodsInfos=groupGoodsVo.getGoodsList();
			if(goodsInfos!=null && goodsInfos.size()>0){
				for(GroupGoodsItemInfo info:goodsInfos){
					info.setPrice("***");
				}
			}
		}
	}
}
